from django.shortcuts import render
from django.http import HttpResponse
from .models import Hexagram,Yao
import random
# Create your views here.
def index(request):
    #算爻
    def yao(use):
        # 分而为二以象两
        left = random.randint(0, use)
        right = use-left
        # 挂一以象三
        first = 1
        left -= 1
        # 揲之以四以象四时
        left_c = left % 4
        if left_c == 0:
            first += 4
            left -= 4
        else:
            first += left_c
            left -= left_c
        right_c = right % 4
        if right_c == 0:
            first += 4
            right -= 4
        else:
            first += right_c
            right -= right_c
        # 归奇于扐以象闺
        return left+right
    #成卦
    def gua():
        use = 49
        for i in range(3):
            use = yao(use)
        return use/4
        # 数——这里输出6个数
    list_n = []
    for i in range(6):
        a = gua()
        list_n.append(a)
    list_n.reverse()
    #本卦
    gua_num = []
    for i in range(len(list_n)):
        g_num = str(round(list_n[i] % 2))
        gua_num.append(g_num)
    gua_str = "" 
    gua_str = gua_str.join(gua_num)
    main_gua = Hexagram.objects.get(num=gua_str)
    #之卦
    zhi_num=[]
    for i in range(len(list_n)):
        if list_n[i] < 8 :
            zhi="1"
        else:
            zhi = "0"
        zhi_num.append(zhi)
    zhi_str=""
    zhi_str=zhi_str.join(zhi_num)
    zhi_gua=Hexagram.objects.get(num=zhi_str)
    #变爻计算
    list_bian = []
    for i in range(len(list_n)):
        if list_n[i] in (6,9) :
            bian = "变"
        else:
            bian = "不变"
        list_bian.append(bian)
    bian_num = list_bian.count("变")
    #组合卦辞
    if bian_num == 0:
        say_gua = "本卦为"+str(main_gua)
        say = "无变爻，取本卦卦辞"
        main_c = Hexagram.objects.values_list("records").filter(num=gua_str)[0][0]
        assist_c = ""
    elif bian_num == 1:
        say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
        say = "有变爻，取本卦变爻爻辞"
        main_c = Yao.objects.values_list("records").filter(hexagram__num=gua_str,yao=6-list_bian.index("变"))[0][0]
        assist_c = ""
    elif bian_num == 2:
        say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
        say = "有二变爻，取本卦变爻爻辞，以上爻为主，下爻为辅"  
        main_c = Yao.objects.values_list("records").filter(hexagram__num=gua_str,yao=6-list_bian.index("变"))[0][0]
        assist_c = Yao.objects.values_list("records").filter(hexagram__num=gua_str,yao=6-max([i for i,x in enumerate(list_bian) if x == "变"]))[0][0]   
    elif bian_num == 3:
        say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
        say = "有三变爻，取本卦卦辞为主，之卦卦辞为辅"  
        main_c = Hexagram.objects.values_list("records").filter(num=gua_str)[0][0]
        assist_c = Hexagram.objects.values_list("records").filter(num=zhi_str)[0][0]
    elif  bian_num == 4:
        say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
        say = "有四变爻，取之卦不变爻爻辞，以下爻为主，上爻为辅"
        main_c = Yao.objects.values_list("records").filter(hexagram__num=zhi_str,yao=6-max([i for i,x in enumerate(list_bian) if x == "不变"]))[0][0]
        assist_c = Yao.objects.values_list("records").filter(hexagram__num=zhi_str,yao=6-list_bian.index("不变"))[0][0]
    elif  bian_num == 5:
        say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
        say = "有五变爻，取之卦不变爻爻辞"
        main_c = Yao.objects.values_list("records").filter(hexagram__num=zhi_str,yao=6-list_bian.index("不变"))[0][0]
        assist_c = ""
    elif bian_num == 6:
        if zhi_str in ("111111","000000"):
            say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
            say ="乾坤两卦，取本卦用爻爻辞"
            main_c = Yao.objects.values_list("records").filter(hexagram__num=gua_str,yao=7)[0][0]
            assist_c = ""
        else :
            say_gua = "本卦为"+str(main_gua)+",之卦为"+str(zhi_gua)
            say = "取之卦卦辞"
            main_c = Hexagram.objects.values_list("records").filter(num=zhi_str)[0]
            assist_c = ""            

    if request.POST:
        q= "本卦问"+request.POST['a']
        return render(request,'yi/detail.html',{"q":q,"say_gua":say_gua,"say":say,"main_c":main_c,"assist_c":assist_c})
    else:
        q = "选一个吧，算的有点准的哟"
        return render(request,'yi/detail.html',{"q":q})

    